package com.whfp.coky.ui.base.vm

import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.viewbinding.ViewBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.whfp.coky.action.StatusAction
import com.whfp.coky.api.response.BaseResult
import com.whfp.coky.api.response.ListBean
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.*
import com.whfp.coky.ui.base.model.RequestBean
import com.whfp.coky.utils.LogUtil
import com.whfp.coky.widget.StatusLayout

/**
 * T表示的是具体操作model
 * recyclerview 基类VM
 * created by coky on 2022/3/8
 */
abstract class RecycleVM<VB : ViewBinding, T> : BaseViewModel<VB>(),
    StatusAction {

    var page: Int = Constants.PAGE_INDEX

    var mListDatas = MutableLiveData<ListBean<T>>()
    var requestMap: HashMap<String, String>? = null
    var mRequest = MutableLiveData<RequestBean>()
    var isLastPage = false

    var mAdapter: BaseAdapter<*, T>? = null

    open fun httpDatas(page: Int) {
        Log.e("tag", "httpDatas...page:${page}...mRequest:${mRequest.value.toString()}")
        launchList({ httpBlock(page) }, mListDatas, false)
    }

    abstract fun buildRequest()

    abstract suspend fun httpBlock(page: Int): BaseResult<ListBean<T>>

    //    监听数据变化
    override fun observe(fragment: BaseFragment<*, *>, owner: LifecycleOwner) {
        //    数据变化
        mListDatas.observe(owner, Observer {
            (fragment as BaseRecycleFrag<*, *, *>).httpFinshed()
            LogUtil.e("mListDatas.observe")
            handleDatas(it.getData())
            isLastPage = it.isLastPage()
        })
        errorData.observe(owner, Observer {
            (fragment as BaseRecycleFrag<*, *, *>).httpFinshed()
            showError(object : StatusLayout.OnRetryListener {
                override fun onRetry(layout: StatusLayout) {
                    onRefresh()
                }
            })
        })

        mRequest.observe(owner, Observer {
            (fragment as BaseRecycleFrag<*, *, *>).mRefreshLayout?.autoRefresh(200)
        })
    }

    override fun observe(activity: BaseActivity<*, *>, owner: LifecycleOwner) {
        //    数据变化
        mListDatas.observe(owner, Observer {
            (activity as BaseRecycleAct<*, *, *>).httpFinshed()
            handleDatas(it.getData())
            isLastPage = it.isLastPage()
        })
        errorData.observe(owner, Observer {
            (activity as BaseRecycleAct<*, *, *>).httpFinshed()
            showError(object : StatusLayout.OnRetryListener {
                override fun onRetry(layout: StatusLayout) {
                    page = Constants.PAGE_INDEX
                    httpDatas(page)
                }
            })
        })

        mRequest.observe(owner, Observer {
            onRefresh()
        })
    }

    fun onRefresh() {
        LogUtil.e("onRefresh......")
        page = Constants.PAGE_INDEX
        isLastPage = false
        httpDatas(page)
    }

    fun onLoadMore(refresh: RefreshLayout) {
        if (isLastPage) {
            refresh.finishLoadMoreWithNoMoreData()
        } else {
            httpDatas(++page)
        }
    }

    fun handleDatas(datas: MutableList<T>?) {
//                判断下拉时
        LogUtil.e("handleDatas.....")
        if (page == Constants.PAGE_INDEX) {
            mAdapter?.clearData()
            if (datas == null || datas.isEmpty()) {
                showEmpty()
            }
        }

        LogUtil.e("mAdapter....null.....${mAdapter == null}")
        mAdapter?.addData(datas)
        LogUtil.e("mAdapter....size.....${mAdapter?.itemCount}")
        showComplete()
    }
}